define(function () {
    return function (pane) {
        /**
         * 导航
         */
        {
            pane.html(utils.getHtml('default_table'));

            utils.addFilterGroupbtn(pane, {
                text: '全部',
                name: 'status',
                filters: dicts.order_status
            });
        }

        /**
         * 统计
         */
        {
            var analyse = function () {
                adminApi('user_orders_analyse').then(function (rows) {
                    $('.content-header .filter-type[data-name="status"] .badge').text('');
                    var total = 0;
                    for (var i in rows) {
                        var row = rows[i];
                        $('.status-' + row.status, pane).text(row.count || 0);
                        total += (row.count || 0);
                    }
                    $('.status-all', pane).text(total);
                });
            }
            analyse();
        }


        //初始化工具
        $(pane).on('click', '.refresh-btn', function () {
            if (table) {
                table.api().draw();
                analyse();
            }
        });

        //初始化表格
        var table = utils.dataTable(pane, {
            columns_arrays: [
                ["id", "#"],
                ["taobaoTradeParentId", "订单号", false],
                ["user_id", "用户", false, function (i, t, row) {
                    var user = row.user;
                    if (!user) {
                        return '';
                    }
                    return (user.image ? '<img src="' + user.image + '?imageView2/1/w/150/h/150/q/75"> ' : '') + '<div>' + (user.nickname || user.tel) + '</div><div>「ID:' + (user.id) + '」</div>' + '<div>订单数: ' + user.order_count + '/' + user.finish_count + '/' + user.sham_count + '&nbsp;&nbsp;金额: ' + user.amount + '/' + user.frozen + '</div>';
                }, 'filterable'],
                ["status", "状态", false, utils.dictRender('order_status')],
                ["created_at", "创建时间", false, utils.dateTimeRender()]
            ],
            sorting: [[0, 'desc']],
            fnServerData: function (paramData) {
                return adminApi('user_orders_browse', paramData);
            }
        });

        //编辑开关
        var toggleHandler = utils.toggleChild(pane, 0, function (checked) {
            table.api().rows().every(function (idx) {
                var child = this.child, data = this.data();
                if (checked == 1) {
                    var opts = [
                        {text: '编辑', icon: 'fa-file-code-o', className: 'edit-opt'},
                        {text: '删除', icon: 'fa-trash-o', className: 'delete-opt btn-danger'}
                    ];
                    child(utils.childWrapper(idx, opts)).show();
                } else {
                    child.hide();
                }
            });
        });

        //初始化事件
        table.on('draw.dt', toggleHandler);

        table.analyse = analyse;
    }
});